var e = require("../common/component"), t = require("./shared");

(0, e.VantComponent)({
    classes: [ "active-class", "toolbar-class", "column-class" ],
    props: Object.assign({}, t.pickerProps, {
        valueKey: {
            type: String,
            value: "text"
        },
        defaultIndex: {
            type: Number,
            value: 0
        },
        columns: {
            type: Array,
            value: [],
            observer: function(newColumns) {
                var isSimple = 0 < arguments.length && void 0 !== arguments[0] ? newColumns : [];
                this.simple = isSimple.length && !isSimple[0].values, this.children = this.selectAllComponents(".van-picker__column"), 
                Array.isArray(this.children) && this.children.length && this.setColumns().catch(function() {});
            }
        }
    }),
    beforeCreate: function() {
        this.children = [];
    },
    methods: {
        noop: function() {},
        setColumns: function() {
            var component = this, data = this.data, columnData = (this.simple ? [ {
                values: data.columns
            } ] : data.columns).map(function(column, index) {
                return component.setColumnValues(index, column.values);
            });
            return Promise.all(columnData);
        },
        emit: function(event) {
            var eventType = event.currentTarget.dataset.type;
            this.simple ? this.$emit(eventType, {
                value: this.getColumnValue(0),
                index: this.getColumnIndex(0)
            }) : this.$emit(eventType, {
                value: this.getValues(),
                index: this.getIndexes()
            });
        },
        onChange: function(event) {
            this.simple ? this.$emit("change", {
                picker: this,
                value: this.getColumnValue(0),
                index: this.getColumnIndex(0)
            }) : this.$emit("change", {
                picker: this,
                value: this.getValues(),
                index: event.currentTarget.dataset.index
            });
        },
        getColumn: function(index) {
            return this.children[index];
        },
        getColumnValue: function(index) {
            var column = this.getColumn(index);
            return column && column.getValue();
        },
        setColumnValue: function(index, value) {
            var column = this.getColumn(index);
            return null == column ? Promise.reject("setColumnValue: 对应列不存在") : column.setValue(value);
        },
        getColumnIndex: function(index) {
            return (this.getColumn(index) || {}).data.currentIndex;
        },
        setColumnIndex: function(index, newIndex) {
            var column = this.getColumn(index);
            return null == column ? Promise.reject("setColumnIndex: 对应列不存在") : column.setIndex(newIndex);
        },
        getColumnValues: function(index) {
            return (this.children[index] || {}).data.options;
        },
        setColumnValues: function(index, values, shouldResetIndex) {
            var shouldReset = !(2 < arguments.length && void 0 !== arguments[2]) || arguments[2], column = this.children[index];
            return null == column ? Promise.reject("setColumnValues: 对应列不存在") : JSON.stringify(column.data.options) === JSON.stringify(values) ? Promise.resolve() : column.set({
                options: values
            }).then(function() {
                shouldReset && column.setIndex(0);
            });
        },
        getValues: function() {
            return this.children.map(function(column) {
                return column.getValue();
            });
        },
        setValues: function(values) {
            var component = this, valuePromises = values.map(function(value, index) {
                return component.setColumnValue(index, value);
            });
            return Promise.all(valuePromises);
        },
        getIndexes: function() {
            return this.children.map(function(column) {
                return column.data.currentIndex;
            });
        },
        setIndexes: function(indexes) {
            var component = this, indexPromises = indexes.map(function(newIndex, index) {
                return component.setColumnIndex(index, newIndex);
            });
            return Promise.all(indexPromises);
        }
    }
});